package cn.iocoder.yudao.module.book.controller.admin.bookshelfcatalogue;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.bookshelfcatalogue.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.bookshelfcatalogue.BookShelfCatalogueDO;
import cn.iocoder.yudao.module.book.service.admin.bookshelfcatalogue.BookShelfCatalogueService;

@Tag(name = "管理后台 - 绘本分类")
@RestController
@RequestMapping("/book/shelf-catalogue")
@Validated
public class BookShelfCatalogueController {

    @Resource
    private BookShelfCatalogueService shelfCatalogueService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本分类")
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:create')")
    public CommonResult<Long> createShelfCatalogue(@Valid @RequestBody BookShelfCatalogueSaveReqVO createReqVO) {
        return success(shelfCatalogueService.createShelfCatalogue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本分类")
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:update')")
    public CommonResult<Boolean> updateShelfCatalogue(@Valid @RequestBody BookShelfCatalogueSaveReqVO updateReqVO) {
        shelfCatalogueService.updateShelfCatalogue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:delete')")
    public CommonResult<Boolean> deleteShelfCatalogue(@RequestParam("id") Long id) {
        shelfCatalogueService.deleteShelfCatalogue(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本分类")
                @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:delete')")
    public CommonResult<Boolean> deleteShelfCatalogueList(@RequestParam("ids") List<Long> ids) {
        shelfCatalogueService.deleteShelfCatalogueListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:query')")
    public CommonResult<BookShelfCatalogueRespVO> getShelfCatalogue(@RequestParam("id") Long id) {
        BookShelfCatalogueDO shelfCatalogue = shelfCatalogueService.getShelfCatalogue(id);
        return success(BeanUtils.toBean(shelfCatalogue, BookShelfCatalogueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本分类分页")
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:query')")
    public CommonResult<PageResult<BookShelfCatalogueRespVO>> getShelfCataloguePage(@Valid BookShelfCataloguePageReqVO pageReqVO) {
        PageResult<BookShelfCatalogueDO> pageResult = shelfCatalogueService.getShelfCataloguePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookShelfCatalogueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本分类 Excel")
    @PreAuthorize("@ss.hasPermission('book:shelf-catalogue:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShelfCatalogueExcel(@Valid BookShelfCataloguePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookShelfCatalogueDO> list = shelfCatalogueService.getShelfCataloguePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本分类.xls", "数据", BookShelfCatalogueRespVO.class,
                        BeanUtils.toBean(list, BookShelfCatalogueRespVO.class));
    }

}